import Vue from 'vue'
import Vuex from 'vuex'
import axios from '../config/axios.js'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        list: []
    },
    mutations: {
        setList(state, data) {
            state.list = data
        },
        // 新增数据
        addArt(state, item) {
            console.log(item);
            state.list.unshift(item)
        },
        // 修改数据
        editArt(state, item) {
            state.list[item.index] = item
        },
        // 点击删除
        deleteArt(state, index) {
            state.list.splice(index, 1)
        },
        // 点击查找
        // searchArt(state, keyWord) {
        //     state.list = state.list.filter(item => {
        //         return item.title.includes(keyWord)
        //     })
        // }
        searchArt(state, keyword) {
            state.list = state.list.filter((item) => {
                return item.title.includes(keyword)
            })
        }
    },
    // 请求的方法
    actions: {
        getList(vuex) {
            axios.get('/list').then(res => {
                console.log(res, vuex);
                vuex.commit('setList', res.data)
            })
        }
    },
    modules: {}
})